#!/usr/bin/python 
#deploy a new vm on cloudstack 
#Author:eager.yu
#Email: sanguren@qq.com

from Cloudstack_option import Cloudstack_option
import sys,re
import getopt


def format_args(args):
    value={}
    if "|" in args:
        args=args.split("|")
        for i in args:
            if re.search("=", i) == None:
                print "value error"
                sys.exit()
            if i == "":
                continue
            elif i.split("=")[0] == "" or i.split("=")[1] == "":
                continue
            a={}
            a[i.split("=")[0]]=i.split("=")[1]
            value.update(a)
    elif not "serviceofferingid" in args or not "templateid" in args or not "zoneid" in args:
            print "args need 'serviceofferingid,templateid and zoneid'"
            return
    elif not "=" in args:
            print "format is not right"
    else:
            if args.split("=")[0] == "" or args.split("=")[1] == "":
                print "args not have value"
            a={}
            a[args.split("=")[0]]=args.split("=")[1]
            print a
            value.update(a)
    return value

def usage():
    print "deployVirtualMachine :"
    print "the -s serviceofferingid ,-t templateid,-z zoneid are need "
    print "command (basic network): deployVirtualMachine.py  -s 27400a20-c914-44bc-8a14-8b799e5b0080 -t aacb8b38-5fd9-4844-86ca-5812565ffe7a -z fc42e7aa-8167-40a7-b84c-ebffd5961203 -a 'name=web06'"
    print "command (Advanced network): deployVirtualMachine.py  -s 27400a20-c914-44bc-8a14-8b799e5b0080 -t aacb8b38-5fd9-4844-86ca-5812565ffe7a -z fc42e7aa-8167-40a7-b84c-ebffd5961203 -a 'name=web06|ipaddress=10.10.0.2|networkids=b641213b-bf8b-40b5-9f40-cc83b40d6caa'"
    print "end ---------------------------------------------------"

def main():
    va_list=["serviceofferingid",
                    "templateid",    
                    "zoneid",
                    "account",
                    "diskofferingid",    
                    "displayname",    
                    "domainid",    
                    "group"    ,
                    "hostid",    
                    "hypervisor",    
                    "ip6address",    
                    "ipaddress",    
                    "iptonetworklist",    
                    "keyboard",    
                    "name",
                    "networkids",    
                    "projectid",    
                    "securitygroupids",    
                    "securitygroupnames",    
                    "size",    
                    "startvm",
                    "userdata",]
      
    serviceofferingid=""
    zoneid=""
    templateid=""
    args=""
    try:
        opts, args = getopt.getopt(sys.argv[1:], "hs:t:z:a:",["help", "serviceofferingid=","templateid=","zoneid=","args="])
    except getopt.GetoptError as err:
        print str(err) 
        usage()
        sys.exit(2)
    for o,a in opts:
        if o in ("-h", "--help"):
            usage()
            sys.exit(0)
        elif o in ("-s", "--serviceofferingid"):
            serviceofferingid=a
        elif o in ("-t", "--templateid"):
            templateid=a
        elif o in ("-z", "--zoneid"):
            zoneid=a
        elif o in ("-a,--args"):
            args=a
        else:
            assert False, "unhandled option"
    if "" in (serviceofferingid,templateid,zoneid):
        usage()
        sys.exit()
    cs=Cloudstack_option()
    args_value=str("serviceofferingid="+serviceofferingid+"|templateid="+templateid+"|zoneid="+zoneid+"|"+args)
    #cs.deploy_instance(args_value)
    value=format_args(args_value)
    for i in value.keys():
        if i not in va_list:
            print "args is not right"
            sys.exit()
    if "ipaddress" in value:
        zone_info=cs.get_zone(zoneid)
        if zone_info[0]["networktype"] == 'Basic':
            print "Basic network can't set ipaddress"
            sys.exit(0) 
    req=cs.deploy_instance(value)
    print req
    sys.exit(0)


if __name__ == "__main__":
    main()